import Vue from 'vue';
import { login } from '@/api/login';
import { setToken, setName } from '@/utils/auth';

import Vuex from 'vuex';
Vue.use(Vuex);
export default new Vuex.Store({
  state: {
    name: '',
    token: ''
  },
  mutations: {
    SET_NAME: (state, name) => {
      state.name = name;
    },
    SET_TOKEN: (state, token) => {
      state.token = token;
      setToken(token);
    }
  },
  actions: {
  // 登录
    Login({ commit }, userInfo) {
      console.log(userInfo);
      const userName = userInfo.name.trim();
      const password = userInfo.password.trim();

      return new Promise((resolve, reject) => {
        login({ userName, password }).then(response => {
          const data = response.data;

          console.log('11');
          commit('SET_NAME', data.userName);
          commit('SET_TOKEN', data.userId);
          setName(data.userName);
          setToken(data.userId);
          resolve(data);
        }).catch(error => {
          reject(error);
        });
      });
    }
    // 注册
    // Regist({ commit }, userInfo) {
    //   const name = userInfo.name.trim();
    //   const password = userInfo.password.trim();

    //   return new Promise((resolve, reject) => {
    //     regist(name, password).then(response => {
    //       const data = response.data;

    //       commit('SET_NAME', data.name);
    //       commit('SET_TOKEN', data.token);
    //       setName(data.name);
    //       setToken(data.token);
    //       resolve(data);
    //     }).catch(error => {
    //       reject(error);
    //     });
    //   });
    // },
    // 登出
    // LogOut({ commit, state }) {
    //   return new Promise((resolve, reject) => {
    //     logout().then(response => {
    //       commit('SET_NAME', '');
    //       commit('SET_TOKEN', '');
    //       setName('');
    //       setToken(false);
    //       // removeName();
    //       // removeToken();
    //       resolve(response);
    //     }).catch(error => {
    //       reject(error);
    //     });
    //   });
    // },
    // 前端 登出
    // FedLogOut({ commit }) {
    //   return new Promise(resolve => {
    //     setToken(false);
    //     commit('SET_TOKEN', false);
    //     resolve();
    //   });
    // }
  },
  getters: {
    name: state => state.name,
    token: state => state.token
  }
});
// const user = {

// }
// export default user

